package com.example.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.font.FontProvider;

import cn.hutool.core.io.FileUtil;

@Component
public class PdfConverter {
	
	@Value("${baseDir}")
	String baseDir;

	public void html2pdf(String htmlFilePath,String pdfFilePath)  {
		try {
	        FileUtil.del(new File(pdfFilePath));
	
	        PdfWriter pdfWriter  = new PdfWriter(new File(pdfFilePath));
	        PdfDocument pdfDocument = new PdfDocument(pdfWriter) ;
	        Document doc = new Document(pdfDocument);
	        pdfDocument.setDefaultPageSize(PageSize.A4);
	        pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
	        ConverterProperties properties =  new ConverterProperties();
	        properties.setBaseUri("https://miro.medium.com/v2/resize:fit:720/format:webp/");
	        FontProvider fontProvider = new DefaultFontProvider();
	        fontProvider.addSystemFonts();
	        //宋体
	        FontProgram fontProgram1 = FontProgramFactory.createFont(baseDir+"simsun.ttc,0");
	        fontProvider.addFont(fontProgram1);
	        properties.setFontProvider(fontProvider);
	        HtmlConverter.convertToPdf(new FileInputStream(htmlFilePath), pdfDocument, properties);
		} catch ( Exception e) {
			throw new RuntimeException(e.getMessage() , e);
		}
		
	}

}

class TextFooterEventHandler implements IEventHandler {
	 
    private Document doc;
    private int page;
 
    public TextFooterEventHandler(Document doc) {
        this.doc = doc;
    }
 
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
        Rectangle pageSize = docEvent.getPage().getPageSize();
        page++;
        canvas.beginText();
        try {
            canvas.setFontAndSize(PdfFontFactory.createFont(), 15);
        } catch (IOException e) {
            e.printStackTrace();
        }
        canvas.moveText((pageSize.getRight() - doc.getRightMargin() + 
                (pageSize.getLeft() + doc.getLeftMargin())) / 2,
                pageSize.getBottom() + doc.getBottomMargin())
        .showText("" + page).endText().release();
    }
 
}
